﻿package org.koyabr.webx.demo.danmaku.dal.dataobjs;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 用户个人资料
 */
public class Profile {
	/**
	 * 所属用户
	 */
	private final User user;
	/**
	 * 邮箱地址
	 */
	private String email;
	/**
	 * 邮箱地址Hash值（提供Gravatar头像服务）
	 */
	private String emailHash;
	/**
	 * 真名
	 */
	private String realname;
	/**
	 * 国籍
	 */
	private String country;

	/**
	 * 构造函数
	 * 
	 * @param user
	 *            个人资料从属用户
	 * 
	 */
	public Profile(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
		// 同时计算新的Hash值
		this.emailHash = MD5Util.md5Hex(email);
	}
	
	public String getEmailHash(){
		return this.emailHash;
	}

	public String getRealname() {
		return realname;
	}
	
	public void setRealname(String realname){
		this.realname = realname;
	}
	
	
	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}


}

/**
 * 
 * 工具类，使用MD5算法，计算字符串的hash值
 *
 */
class MD5Util {
	public static String hex(byte[] array) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < array.length; ++i) {
			sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(
					1, 3));
		}
		return sb.toString();
	}

	public static String md5Hex(String message) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			return hex(md.digest(message.getBytes("CP1252")));
		} catch (NoSuchAlgorithmException e) {
		} catch (UnsupportedEncodingException e) {
		}
		return null;
	}
}
